﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace HMIWebDisplay
{
    public class DisplayBinding
    {
        public string DisplayReferenceID { get; private set; }
        public string DisplayBindingID { get; private set; }
        public DisplayElement DisplayElement { get; set; }
        

        public static IEnumerable<DisplayBinding> ParseXDocument(XDocument xDocument)
        {
            return xDocument.Descendants("dataobject")
                .Select(x =>
                    {
                        var parent = x.Parent;
                        if (parent != null)
                        {
                            var referenceID = x.Attribute("objectid");
                            var bindingID = parent.Attribute("ID");
                            if (referenceID != null && bindingID != null)
                                return new DisplayBinding
                                    {
                                        DisplayReferenceID = referenceID.Value,
                                        DisplayBindingID = bindingID.Value
                                    };

                        }
                        return null;
                    })
                .Where(x => x != null);
        }

    }
}
